package com.chanyachting.emall.serviceImpl;

import java.util.List;

import org.hibernate.Query;

import com.chanyachting.emall.dao.SellyachtDAO;
import com.chanyachting.emall.domain.Sellyacht;
import com.chanyachting.emall.service.SellyachtService;

public class SellyachtServiceImpl implements SellyachtService {

	private SellyachtDAO cDao;

	public SellyachtServiceImpl() {

		this.cDao = new SellyachtDAO();
	}
	
	public int QueryAllSellyachtCountByChannelId(String channelId) {
		String sql = "select count(*) from Sellyacht where recommend = '1' and channelId in(" + channelId + ")";
		
		return (Integer)this.cDao.find(sql).get(0);
	}
	
	@SuppressWarnings("unchecked")
	public List<Sellyacht> QueryAllSellyachtByChannelId(int currentPage,int pageSize,String channelId) {
		String sql = "from Sellyacht where recommend = '1' and channelId in (" + channelId + ")";
		sql = sql + " order by updateDate desc";
		Query q = this.cDao.createQuery(sql);
		q.setMaxResults(pageSize);
		q.setFirstResult((currentPage -1) * pageSize);
		
		return q.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<Sellyacht> QueryAppointSellYacht(int currentPage,int pageSize,String productCategoryId) {
		String sql= "from Sellyacht where " +
				"status = '01' and productCategoryId = '" + productCategoryId +"'";
		Query q = this.cDao.createQuery(sql);
		q.setMaxResults(pageSize);
		q.setFirstResult((currentPage -1) * pageSize);
		
		return q.list();
	}
	
	public int QueryAppointSellYachtCount(String status,String productCategoryId) {
		String sql = "select count(*) from Sellyacht " +
			"where status = ? and productCategoryId = ?";
		
		return (Integer)this.cDao.find(sql, new String[]{status,productCategoryId}).get(0);
	}
	
	@SuppressWarnings("unchecked")
	public Sellyacht QueryOneSellyacht(String productId) {
		String sql = "from Sellyacht where id = ?";
		
		List<Sellyacht> l = this.cDao.find(sql, new String[]{productId});
		
		if (l == null || l.size() != 1) {
			return null;
		}
		return l.get(0);
	}
	
	@SuppressWarnings("unchecked")
	public List<Sellyacht> QuerySellyachtByCompanyId(String companyId) {
		String sql = "from Sellyacht where companyId = ? and status = '01'";
		
		List<Sellyacht> l = this.cDao.find(sql, new String[]{companyId});
		
		return l;
	}
	
	public int getSellyachtCount(String companyId) {
		StringBuffer sb = new StringBuffer();
		
		sb.append("select count(*) from Sellyacht where status = '01'");
		
		if (companyId == null || "".equals(companyId)) {
			sb.append(" and companyId = " + companyId);
		}
		
		return (Integer)this.cDao.find(sb.toString()).get(0);
	}
	
	@SuppressWarnings("unchecked")
	public List<Sellyacht> getSellyachtListByPage(int currentPage,int pageSize,String companyId) {
		StringBuffer sb = new StringBuffer();
		
		sb.append("from Sellyacht where status = '01'");
		
		if (companyId == null || "".equals(companyId)) {
			sb.append(" and companyId = " + companyId);
		}
		
		Query q = this.cDao.createQuery(sb.toString());
		
		q.setFirstResult((currentPage - 1) * pageSize);
		q.setMaxResults(pageSize);
		
		return q.list();
	}
}
